package cn.anxingongyou.anxin.controller;

import cn.anxingongyou.anxin.entity.JobCategory;
import cn.anxingongyou.anxin.service.JobCategoryService;
import cn.anxingongyou.anxin.utils.RedisUtil;
import cn.anxingongyou.anxin.utils.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class JobCategoryController {

    private final JobCategoryService jobCategoryService;
    @Autowired
    public JobCategoryController(JobCategoryService jobCategoryService) {
        this.jobCategoryService = jobCategoryService;
    }

    @Resource
    private RedisUtil redisUtil;

    @ApiOperation("添加工作信息")
    @ApiResponses({
            @ApiResponse(code = 201,message = "添加成功"),
            @ApiResponse(code = 500,message = "添加失败")
    })
    @RequestMapping(value = "/job/add",method = RequestMethod.POST)
    public Map addJob(
            @RequestBody @ApiParam(value = "工作信息", required = true)JobCategory jobCategory
            ){
        String key = "jobList";
        if(redisUtil.hasKey(key)){
            // 存在缓存 删除缓存
            redisUtil.del(key);
        }
        jobCategory.setCreated(new Date());
        jobCategory.setUpdated(new Date());
        int res = jobCategoryService.insert(jobCategory);
        if(res > 0) {
            return Result.instance(201,"添加成功").toJsonMap();
        } else {
            return Result.instance(500,"添加失败").toJsonMap();
        }
    }

    @ApiOperation("删除工作信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "删除成功"),
            @ApiResponse(code = 500,message = "删除失败")
    })
    @RequestMapping(value = "/job/del/{id}",method = RequestMethod.GET)
    public Map delJob(
            @PathVariable @ApiParam(value = "工作主键", required = true) Long id
    ){
        String key = "jobList";
        if(redisUtil.hasKey(key)){
            // 存在缓存 删除缓存
            redisUtil.del(key);
        }
        int res = jobCategoryService.deleteByPrimaryKey(id);
        if(res > 0) {
            return Result.instance(204,"删除成功").toJsonMap();
        } else {
            return Result.instance(500,"删除失败").toJsonMap();
        }
    }

    @ApiOperation("更新工作信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "更新成功"),
            @ApiResponse(code = 500,message = "更新失败")
    })
    @RequestMapping(value = "/job/update",method = RequestMethod.POST)
    public Map updateJob(
            @RequestBody @ApiParam(value = "工作信息,必传主键", required = true) JobCategory jobCategory
    ){
        String key = "jobList";
        if(redisUtil.hasKey(key)){
            // 存在缓存 删除缓存
            redisUtil.del(key);
        }
        int res = jobCategoryService.updateByPrimaryKeySelective(jobCategory);
        if(res > 0) {
            return Result.instance(204,"更新成功").toJsonMap();
        } else {
            return Result.instance(500,"更新失败").toJsonMap();
        }
    }

    @ApiOperation("获取工作信息通过信息")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/job/findByJob",method = RequestMethod.POST)
    public Map findOneJob(
            @RequestBody @ApiParam(value = "工作信息", required = true) JobCategory jobCategory
    ){
        JobCategory jobCategory1 = jobCategoryService.selectOne(jobCategory);
        if(jobCategory1 != null) {
            return Result.<JobCategory>instance(200,"获取成功",jobCategory1).toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("获取工作信息通过主键")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/job/findOneById/{id}",method = RequestMethod.GET)
    public Map findOneById(
            @PathVariable @ApiParam(value = "工作主键", required = true) Long id
    ){
        JobCategory jobCategory = jobCategoryService.selectOneByPrimaryKey(id);
        if(jobCategory != null) {
            return Result.<JobCategory>instance(200,"获取成功",jobCategory).toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("获取所有工作类型")
    @ApiResponses({
            @ApiResponse(code = 200,message = "查询成功"),
            @ApiResponse(code = 500,message = "查询失败")
    })
    @RequestMapping(value = "/job/page/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Map selectAddressByJob(
            @PathVariable("pageNum") @ApiParam(value = "第几页", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页多少条", required = true) int pageSize,
            @RequestBody @ApiParam(value = "工作信息", required = true)  JobCategory jobCategory){
        PageInfo<JobCategory> pageInfo = jobCategoryService.page(pageNum, pageSize, jobCategory);
        Map<String,Object> res = new HashMap<>();
        res.put("total", pageInfo.getTotal());
        res.put("pageNum", pageInfo.getPageNum());
        res.put("pageSize", pageInfo.getPageSize());
        res.put("list",pageInfo.getList());
        return Result.<Map>instance(200,"查询成功",res).toJsonMap();
    }

    @ApiOperation("api获取所有工作类型")
    @ApiResponses({
            @ApiResponse(code = 200,message = "查询成功"),
            @ApiResponse(code = 500,message = "查询失败")
    })
    @RequestMapping(value = "/api/job/page/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Map apiSelectByJob(
            @PathVariable("pageNum") @ApiParam(value = "第几页", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页多少条", required = true) int pageSize,
            @RequestBody @ApiParam(value = "工作信息", required = true)  JobCategory jobCategory){
        String key = "jobList";
        if(redisUtil.hasKey(key)){
            Map<String,Object> res =  (Map<String,Object>)  redisUtil.get(key);
            return Result.<Map>instance(200,"查询成功",res).toJsonMap();
        }else {
            PageInfo<JobCategory> pageInfo = jobCategoryService.page(pageNum, pageSize, jobCategory);
            Map<String,Object> res = new HashMap<>();
            res.put("total", pageInfo.getTotal());
            res.put("pageNum", pageInfo.getPageNum());
            res.put("pageSize", pageInfo.getPageSize());
            res.put("list",pageInfo.getList());
            redisUtil.set(key,res);
            return Result.<Map>instance(200,"查询成功",res).toJsonMap();
        }
    }

}
